﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Xml.Linq;
using HotelManager.DataAccess.Model;

namespace HotelManager.DataAccess.Helpers
{
    public class XmlHelper
    {
        public HotelInfosModel GetHotelDescription(int hotelId)
        {
            // Create model
            HotelInfosModel model = new HotelInfosModel();

            using (var context = new HotelDbEntities())
            {
                // Get hotel by id
                Hotel hotel = context.Hotels.FirstOrDefault(x => x.Id == hotelId && x.IsActive);
                if (hotel != null)
                {
                    // Get description
                    string description = hotel.Description;
                    if (description == null)
                    {
                        List<string> tmp = new List<string> {"No information is available!"};
                        string key = "No information";
                        model.Descriptions = new Dictionary<string, List<string>> {{key, tmp}};
                        model.Facilities = new Dictionary<string, List<string>> {{key, tmp}};
                        return model;
                    }

                    // TODO: Validate schema

                    // Parse
                    XElement xElement = XElement.Parse(description);

                    // Get descriptions
                    model.Descriptions = GetDescriptions(xElement);

                    // Get facilities
                    model.Facilities = GetFacilities(xElement);
                }
            }
            return model;
        }

        private Dictionary<string, List<string>> GetDescriptions(XElement xElement)
        {
            // Create dictionary
            var description = new Dictionary<string, List<string>>();

            // Get description
            XElement xDescription = xElement.Element("Description");

            // Get list of item
            IEnumerable<XElement> items = xDescription.Elements("Item");

            foreach (XElement item in items)
            {
                // Create list of values
                List<string> valueList = new List<string>();

                // Get item's name
                string name = item.Element("Name").Value;

                // Get list of values
                IEnumerable<XElement> values = item.Element("Values").Elements("Value");

                foreach (XElement value in values)
                {
                    // Add value to list
                    valueList.Add(value.Value);
                }
                description.Add(name, valueList);
            }
            return description;
        }

        private Dictionary<string, List<string>> GetFacilities(XElement xElement)
        {
            // Create dictionary
            var facilities = new Dictionary<string, List<string>>();

            // Get facilities
            XElement xDescription = xElement.Element("Facilities");

            // Get list of item
            IEnumerable<XElement> items = xDescription.Elements("Item");

            foreach (XElement item in items)
            {
                // Create list of values
                List<string> valueList = new List<string>();

                // Get item's name
                string name = item.Element("Name").Value;

                // Get list of values
                IEnumerable<XElement> values = item.Element("Values").Elements("Value");

                foreach (XElement value in values)
                {
                    // Add value to list
                    valueList.Add(value.Value);
                }
                facilities.Add(name, valueList);
            }
            return facilities;
        }
    }
}
